﻿
@using IOA.MES.DataAccess.VModel
@{
    ViewBag.Title = "产线库存查询";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workshops = ViewBag.Workshops as List<VWorkshop>;
}

@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}
<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card layui-form">
            <div class="layui-card-body">
                <div class="layui-form-item layui-row">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">车间：</label>
                        <div class="layui-input-inline">
                            <select lay-filter="workshopCode" name="workshopCode">
                                <option value="">请选择</option>
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.AssemblyLine">流水线：</label>
                        <div class="layui-input-inline">
                            <select lay-filter="lineId" name="lineId">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <select lay-filter="workOrderNo" name="workOrderNo" lay-search>
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.StorageType">库存量：</label>
                        <div class="layui-input-inline">
                            <select lay-filter="hasStorage" name="hasStorage">
                                <option value="">全部</option>
                                <option value="1" selected>有存库</option>
                                <option value="0">无库存</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.IssueBillNo">发料单号：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="billNo" placeholder="请输入发料单号" class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-list" type="button" lay-submit lay-filter="submit">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>


@section Scripts{
    <script>
        var workshops = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(workshops));
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['form', 'layer', 'index', 'laydate', 'table'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form;

                var options = {
                    elem: '#table'
                    , url: '/WorkOrder/QueryLineStoragess'
                    , method: "post"
                    , where: { workshopCode: "", lineId: 0, workOrderNo: "", hasStorage: true, billNo: "" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { type: 'numbers', title: '序号', fixed: 'left', width: 60 }
                        , { field: 'LineName', title: '产线', width: 80, fixed: 'left' }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 170, fixed: 'left' }
                        , { field: 'CategoryName', title: '物料分类', width: 100, fixed: 'left' }
                        , { field: 'BillNo', title: '发料单号', minWidth: 150 }
                        , { field: 'ProductCode', title: '物料编码', minWidth: 200 }
                        , { field: 'ProductName', title: '物料名称', minWidth: 200 }
                        , { field: 'ProductSpecs', title: '规格', minWidth: 200 }
                        , { field: 'AllocateNo', title: '调拨单号', minWidth: 200 }
                        , { field: 'ProductCompany', title: '生产厂家', minWidth: 200 }
                        , { field: 'BatchNo', title: '批次号', minWidth: 200 }
                        , { field: 'BatchNoBig', title: '托盘号', minWidth: 200 }
                        , { field: 'BatchNoSmall', title: '箱号', minWidth: 200 }
                        , { field: 'OriginQuantity', width: 100, fixed: 'right', title: '发料数量', style: 'color:red;' }
                        , { field: 'Quantity', width: 100, fixed: 'right', title: '剩余数量', style: 'color:red;' }
                    ]]
                };

                queryTable();
                form.on("submit(submit)", function (data) {
                    var forms = data.field;
                    forms.lineId = forms.lineId == "" ? 0 : parseInt(forms.lineId);
                    forms.hasStorage = forms.hasStorage == "" ? null : (parseInt(forms.hasStorage) == 1);
                    queryTable(data.field);
                });
                function queryTable(forms) {
                    options.page.curr = 1;
                    if (forms) {
                        options.where = forms;
                    }
                    if (table.index > 0) {
                        table.reload("table", options);
                    } else {
                        table.render(options);
                    }
                }

                form.on('select(workshopCode)', function (data) {
                    var workshop = workshops.find((e) => e.WorkshopCode == data.value);
                    var options = ["<option value=''>请选择</option>"];
                    for (var i = 0; i < workshop.Lines.length; i++) {
                        options.push("<option value='" + workshop.Lines[i].PkId + "'>" + workshop.Lines[i].LineName + "</option>");
                    }
                    $("select[name=lineId]").html(options.join(""));
                    form.render();
                    queryWorkOrders();
                });

                function queryWorkOrders() {
                    var loadId = layer.load();
                    $.ajax({
                        url: "/WorkOrder/QueryWorkshopWorkOrderNos",
                        type: "POST",
                        data: {
                            workshopCode: $("select[name=workshopCode]").val().trim()
                        },
                        complete: function () {
                            layui.layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layui.layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var options = ["<option value=''>请选择</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                options.push("<option value='" + res.Data[i] + "'>" + res.Data[i] + "</option>");
                            }
                            $("select[name=workOrderNo]").html(options.join(""));
                            form.render();
                        }
                    });
                }
            });
        }
    </script>
}
